<?php
/**
 * Created by PhpStorm.
 * User: lgm
 * Date: 2018/9/7
 * Time: 14:08
 */

namespace app\index\controller;

use think\captcha\Captcha;
use think\facade\Cookie;
use think\facade\Request;
use think\Validate;

class Login extends MyController
{
    public function login()
    {
        if (Request::isPost()){
            $code = Request::post('code');
            $username = Request::post('username');
            $password =  Request::post('password');
            //验证数据
            $rule = [
                'code'=>'require|captcha',
                'username'=>'require'
            ];
            $msg = [
                'code.require'=>'验证码不能为空',
                'code.captcha'=>'验证码不正确',
                'username.require'=>'请输入您的账号',
            ];
            $data = [
                'code'=>$code,
                'username'=>$username
            ];
            $validate   = Validate::make($rule,$msg);
            $result = $validate->check($data);
            if ($result){
                $patternm ="/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/"; //电话号码正则
                $patterne = "/^([a-z0-9]+[-_\.]?[a-z0-9]+)*@([a-z0-9]+)\.([a-z0-9]{1,})$/i";  //邮箱正则
                if (!preg_match_all($patternm,$username,$matches)) { //不是电话号码，有可能是邮箱或者用户名
                    if (!preg_match_all($patterne,$username,$matches)){//不是邮箱，那就是用户名
                        $select = db('user')->field('password,verify,phone')->where('username',$username)->find();
                    }else{ //是邮箱
                        $select = db('user')->field('password,verify,phone')->where('email',$username)->find();
                    }
                }else{//是电话码号
                    $select = db('user')->field('password,verify,phone')->where('phone',$username)->find();
                }
                //数据对比
                $password =md5(md5($password).$select['verify']);
                if ($password == $select['password']){
                    if (Request::post('chbox')=='on'){
                        cookie('phone',$select['phone'],2*7*24*60*60);//2周内自动登录
                    }else{
                        cookie('phone',$select['phone'],2*60*60);
                    }
                    jump('登录成功','/index/index');
                }else{
                    jump('账户或密码错误');
                }
            }else{
                jump($validate->getError());
            }
        }
    }
    //注册
    public function register(){
        if (Request::isAjax()){
            $phone = Request::param('phone');
            $res = db('user')->where('phone',$phone)->value('u_id');
            if (empty($res)){
                $rule = [
                    'mobile'=>'mobile'
                ];
                $msg = [
                    'mobile' =>'无效的手机号码'
                ];
                $data = [
                    'mobile'=>$phone
                ];
                $validate   = Validate::make($rule,$msg);
                $result = $validate->check($data);

                if(!$result) {
                    echo 1;exit;//无效手机号码
                }else{
                    echo 2;exit;//正常
                }
            }else{
                echo 0;exit;//手机号码被占用
            }
        }
        if (Request::isPost()){
            //接收数据
            $form['phone'] = Request::post('phone');
            $form['username'] = md5(uniqid(mt_rand(), true));
            $password = Request::post('password');
            $repass = Request::post('repass');
            //验证数据
            $rule = [
                'mobile'=>'require|mobile',
                'password'=>'require|length:6,24',
                'repass'=>'require|confirm:password'
            ];
            $msg = [
                'mobile.require'=>'手机号码不能为空',
                'mobile' =>'无效的手机号码',
                'password.require'=>'密码不能为空',
                'password.length'=>'密码少于6个字符或者超过了24个字符',
                'repass.require' =>'密码与确认密码不一致',
                'repass.confirm' =>'密码与确认密码不一致'
            ];
            $data = [
                'mobile'=>$form['phone'],
                'password'=>$password,
                'repass'=>$repass,
            ];
            $validate   = Validate::make($rule,$msg);
            $result = $validate->check($data);

            //密码加盐
            $form['verify'] = md5(rand(10000,99999));
            $form['password'] = md5(md5($password).$form['verify']);
            $form['regtime'] = time();
            if($result) {
                if (db('user')->insert($form)){
                    $this->success('注册成功,可以前往登录啦！','/index/index/index');
                }else{
                    $this->success('注册失败');
                }
            }else{
                jump($validate->getError());
            }
        }
    }

    public function logout(){
        cookie('phone', null);
        return redirect('/index/index');
    }
    /**
     * 自定义验证码用法
     */
    public function verify(){
        $config =    [
            // 验证码字体大小
            'fontSize' =>18,
            'imageW' =>120,
            'imageH' =>35,
            // 验证码位数
            'length' => 4,
            //是否画混淆曲线
            'useCurve' => false,
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
}
